// 导入axios
import axios from 'axios'
// 导入进度条插件
import NProgress from 'nprogress'
// 导入进度条样式
import 'nprogress/nprogress.css'
// 配置请求的基准URL地址
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'

// axios设置请求拦截器
axios.interceptors.request.use(
  config => {
    // 发请求开启进度条
    NProgress.start()
    //  设置响应头
    config.headers.Authorization = window.sessionStorage.getItem('token')
    return config
  },
  err => {
    console.log(err)
  }
)
// 响应拦截器
// axios设置响应拦截器
axios.interceptors.response.use(config => {
  // 关闭进度条
  NProgress.done()
  return config
}, err => {
  console.log(err)
})

// 登录请求
const login = (data) => {
  return axios.post('login', data)
}

// ===============users相关============================
// 获取用户列表
const getUsers = (data) => {
  return axios.get('users', data)
}
// 修改状态请求
const setStatus = (id, state) => {
  return axios.put(`users/${id}/state/${state}`)
}
// 添加用户操作
const addUsers = (data) => {
  return axios.post(`users`, data)
}
// 编辑用户请求
const editUsers = (id, data) => {
  return axios.put(`users/${id}`, data)
}
// 删除用户请求
const deleteUsers = (id) => {
  return axios.delete(`users/${id}`)
}

// =====================权限相关=========================
// 获取角色权限列表
const getRoles = () => {
  return axios.get(`roles`)
}
// 获取权限树
const getpowerTree = (type) => {
  return axios.get(`rights/${type}`)
}
// 点击三级权限删除
const deleteRighes3 = (roleId, rightId) => {
  return axios.delete(`roles/${roleId}/rights/${rightId}`)
}
// 角色授权
const setRolesPower = (roleId, rids) => {
  return axios.post(`roles/${roleId}/rights/`, {
    rids
  })
}
// 请求所有角色列表
const getAllRoles = () => {
  return axios.get(`roles`)
}
// 更新角色
const setNewRoles = (id, rid) => {
  return axios.put(`users/${id}/role`, {
    'rid': rid
  })
}
// =================goods相关======================
// 获取所有goods列表
const getGoods = (data) => {
  return axios.get(`goods`, {
    params: data
  })
}
// 根据id查询商品
const getGoodsById = (id) => {
  return axios.get(`goods/${id}`)
}
// 获取goods分类列表
const getGoodsCategory = (data) => {
  return axios.get(`categories`, {
    params: data
  })
}
// 添加分类请求
const addCategoris = (data) => {
  return axios.post(`categories`, data)
}
// 获取分类参数列表
const getGoodsAttributes = (id, sel) => {
  return axios.get(`categories/${id}/attributes`, {
    params: {
      sel
    }
  })
}
// 添加参数
const addAttributes = (id, data) => {
  return axios.post(`categories/${id}/attributes`, data)
}
// 删除参数
const deleteAttributes = (id, attrid) => {
  return axios.delete(`categories/${id}/attributes/${attrid}`)
}
// 编辑提交参数
const editAttributes = (id, attrid, data) => {
  return axios.put(`categories/${id}/attributes/${attrid}`, data)
}
// 根据id查询参数
const getAttrById = (id, attrid, sel, vals) => {
  return axios.get(`categories/${id}/attributes/${attrid}`, {
    params: {
      attr_sel: sel,
      attr_vals: vals
    }
  })
}
// 添加商品
const addGoods = (data) => {
  return axios.post('goods', data)
}
// 订单相关===========================================
// 获取订单列表
const orderList = (data) => {
  return axios.get('orders', {
    params: data
  })
}
// =================数据统计===================
const getrepotrs = () => {
  return axios.get('reports/type/1')
}
export default {
  login,
  getUsers,
  setStatus,
  addUsers,
  editUsers,
  deleteUsers,
  getRoles,
  deleteRighes3,
  getpowerTree,
  setRolesPower,
  getAllRoles,
  setNewRoles,
  getGoods,
  getGoodsCategory,
  addCategoris,
  getGoodsAttributes,
  addAttributes,
  deleteAttributes,
  editAttributes,
  getGoodsById,
  getAttrById,
  addGoods,
  orderList,
  getrepotrs
}
